package com.woniu.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.exception.userexp.YogaException;
import com.woniu.model.Result;
import com.woniu.order.Form.AddLeagueClassDetailForm;
import com.woniu.order.Form.AddMemberCardOrderForm;
import com.woniu.order.Param.OrderPayParam;
import com.woniu.order.dto.LeagueClassDetailDto;
import com.woniu.order.service.MemberCardOrderService;
import com.woniu.util.JwtUtil;
import com.woniu.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@RestController
@RequestMapping("/memberCardOrder")
@Api(tags ="会员卡订单管理")
public class MemberCardOrderController {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MemberCardOrderService memberCardOrderService;


    /**
     * 新增会员卡订单
     * @param form
     * @return
     * @throws YogaException
     */
    @PostMapping("/addMemberCardOrder")
    @ApiOperation(value = "新增会员卡订单")
    public Result addMemberCardOrder(@RequestBody AddMemberCardOrderForm form) throws Exception {

        memberCardOrderService.addMemberCardOrder(form);
        return Result.getResult().setMsg("新增成功");
    }
    @GetMapping("/pay")
    @ApiOperation(value = "发起支付")
    public Result pay(String orderNum, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);//从短token取出键
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("userId").toString());

        OrderPayParam orderPayParam = new OrderPayParam();
        orderPayParam.setOrderNum(orderNum);
        orderPayParam.setUserId(userId);

        return Result.getResult(null).setCode(200).setMsg("发起支付成功").setData(memberCardOrderService.pay(orderPayParam));
    }
    @PostMapping("/notify")
    @ApiOperation(value = "给支付宝回调的接口")
    public String payNotify(HttpServletRequest request) throws Exception {
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        memberCardOrderService.payNotify(params);
        return "SUCCESS";
    }


    /**
     * 新增团课详细跟踪
     * @param form
     * @return
     * @throws YogaException
     */
   // @PostMapping("/addLeagueClassDetail")
    //@ApiOperation(value = "新增团课详细跟踪")
    public Result addLeagueClassDetail(@RequestBody AddLeagueClassDetailForm form) throws YogaException {

        memberCardOrderService.addLeagueClassDetail(form);
        return Result.getResult().setMsg("新增成功");
    }


    /**
     * 完成团课详细
     * @param leagueClassWorkforceId
     * @return
     * @throws YogaException
     */
   //@GetMapping("/finishLeagueClassDetail")
   // @ApiOperation(value = "完成团课详细")
    public Result finishLeagueClassDetail(Integer leagueClassWorkforceId) throws YogaException {

        memberCardOrderService.finishLeagueClassDetail(leagueClassWorkforceId);
        return Result.getResult().setMsg("完成当堂课成功");
    }

    /**
     * 取消团课详细
     * @param leagueClassWorkforceId
     * @return
     */
   // @GetMapping("/cancelLeagueClassDetail")
    //@ApiOperation(value = "取消团课详细")
    Result<?> cancelLeagueClassDetail( Integer leagueClassWorkforceId){
        memberCardOrderService.cancelLeagueClassDetail(leagueClassWorkforceId);
        return Result.getResult().setMsg("取消团课详情成功");
    }
     @GetMapping("/cancelLeagueClassDetail")
    @ApiOperation(value = "取消会员卡")
    Result<?> Membershipcardcancellation( Integer menberuserid){
        memberCardOrderService.membershipcardcancellation(menberuserid);
        return Result.getResult().setMsg("取消会员卡成功");
    }

    /**
     * 用户遍历自己的团课
     * @param classState 单堂课程状态
     * @param token
     * @return
     */
   // @GetMapping("/loadLeagueClassDetailByUser")
    //@ApiOperation(value = "用户遍历自己的团课")
    Result<List<LeagueClassDetailDto>> loadLeagueClassDetailByUser(@RequestHeader String token, Integer classState, String leagueClassName) throws Exception {
        String s = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseUserToken(s).get("userId").toString());

        List<LeagueClassDetailDto> list = memberCardOrderService.loadLeagueClassDetailByUser(userId,classState,leagueClassName);

        return Result.getListResult(LeagueClassDetailDto.class).setMsg("查询团课详情成功");
    }

}

